//
//  SWAlbumCell.swift
//  MiU
//
//  Created by linhan on 16/3/28.
//  Copyright © 2016年 MiU. All rights reserved.
//

import Foundation
import UIKit
class SWAlbumCell: UITableViewCell,SWALAlbumDelegate
{
    weak var album:SWALAlbum? {
        didSet {
            album?.delegate = self
            //layoutSubviews()
        }
    }
    
    private var _cellWidth:CGFloat = 0
    
    lazy private var _imageView:UIImageView = UIImageView()
    lazy private var _nameLabel:UILabel = UILabel()
    
    init(style: UITableViewCellStyle, reuseIdentifier: String?, cellWidth:CGFloat)
    {
        _cellWidth = cellWidth
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setup()
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setup()
    {
        let Grid:CGFloat = 9
        let Size:CGSize = CGSizeMake(70, 70)
        _imageView.frame = CGRectMake(Grid, (SWThemeStyle.CameraRollCellHeight - Size.height) * 0.5, Size.width, Size.height)
        _imageView.clipsToBounds = true
        _imageView.contentMode = .scaleAspectFill
        contentView.addSubview(_imageView)
        
        
        let NameLabelX = _imageView.right + Grid
        let NameLabelRightMargin:CGFloat = 30
        let NameLabelHeight:CGFloat = 19
        _nameLabel.font = SWAppearanceManager.themeStyle.fontOfSize(15)
        _nameLabel.textColor = UIColor.lightGray
        _nameLabel.frame = CGRectMake(NameLabelX, (SWThemeStyle.CameraRollCellHeight - NameLabelHeight) * 0.5, _cellWidth - NameLabelRightMargin - NameLabelX, NameLabelHeight)
        contentView.addSubview(_nameLabel)
    }
    
    override func layoutSubviews()
    {
        super.layoutSubviews()
        
        if let album = album
        {
            _imageView.image = album.thumbImage
            _nameLabel.text = album.name
        }
    }
    
    func albumCoverChanged(_ album:SWALAlbum, cover:UIImage)
    {
        _imageView.image = cover
    }
    
    
}
